<template>
  <el-dialog
    title="结款"
    center
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule"  ref="dataForm" label-width="120px">
      <el-form-item label="单位名字" prop="accountUnitName">
        <el-input disabled v-model="dataForm.accountUnitName" placeholder="挂账单位名字"></el-input>
      </el-form-item>
      <el-form-item label="未结金额" prop="accountUnitTel">
        <el-input disabled v-model="dataForm.notSettlementTotal" placeholder="单位电话"></el-input>
      </el-form-item>
      <el-form-item label="本次结款金额" prop="money">
        <el-input  v-model="dataForm.money" placeholder="本次结款金额"></el-input>
      </el-form-item>

      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="dataForm.payMethod" placeholder="支付方式">
          <el-option label="现金" value="现金"></el-option>
          <el-option label="支付宝" value="支付宝"></el-option>
          <el-option label="微信" value="微信"></el-option>
          <el-option label="银行卡" value="银行卡"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结款人" prop="settlementLink">
        <el-input  v-model="dataForm.settlementLink" placeholder="结款人"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="settlementTel">
        <el-input  v-model="dataForm.settlementTel" placeholder="电话"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {isNumber,keepTwoPoint} from '../../../../../utils/index'
import {isMobile} from '../../../../../utils/validate'
export default {
  name: "checkOut",
  data(){
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return{
      visible:false,
      dataForm:{
        money:0
      },
      disabledSubmit:false,
      dataRule: {
        money:[
          { required: true, message: '金额不能为空', trigger: 'blur' }
        ],
        settlementLink:[
          { required: true, message: '结款人不能为空', trigger: 'blur' }
        ],
        payMethod:[
          { required: true, message: '支付方式不能为空', trigger: 'blur' }
        ],
        settlementTel:[
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      }
    }
  },

  methods:{
    init(row){
      this.dataForm = row
      this.visible = true
    },

    dataFormSubmit(){
      if (isNumber(this.dataForm.money)){
        if (this.dataForm.money > this.dataForm.notSettlementTotal){
          this.$message.info("结款金额不能大于未结金额")
        }
        else if (this.dataForm.money <= 0){
          this.$message.info("结款金额必须大于0")
        }
        else {
          this.$http({
            url: this.$http.adornUrl(`/store/tstoreaccountunit/checkOut`),
            method: 'post',
            data: this.$http.adornData({
              'accountUnitId': this.dataForm.accountUnitId || undefined,
              'notSettlementTotal': this.dataForm.notSettlementTotal,
              'money': Number(keepTwoPoint(this.dataForm.money)),
              'payMethod': this.dataForm.payMethod,
              'settlementLink': this.dataForm.settlementLink,
              'settlementTel': this.dataForm.settlementTel
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '结款成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.disabledSubmit = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      }else {
        this.$message.info("请输入合法数")
        this.dataForm.money = ''
      }
    }
  },


}
</script>

<style scoped>

</style>
